export const deepCopy = (obj: any) => {
  return JSON.parse(JSON.stringify(obj))
}

/**
 *
 * @param {*} tree 树结构
 * @param {*} cb 每个节点的回调函数，参数为当前节点
 */
export function forEachTree(tree: any[], cb: (node: any) => {}, childrenKey: string = 'children') {
  let root = {}
  if (Array.isArray(tree)) {
    root[childrenKey] = tree
  } else {
    root = tree
  }

  cb(root)

  function loop(t: any) {
    const children = t[childrenKey]
    if (children && children.length > 0) {
      children.forEach((d: any) => {
        cb(d)
        loop(d)
      })
    }
  }

  loop(root)
}

export function formSession(key: string, form?: any) {
  const dataStr = sessionStorage.getItem(key)

  let dataObj: any = {}
  if (dataStr) {
    dataObj = JSON.parse(dataStr)
  }

  return {
    get(): any {
      console.log()

      return dataObj
    },
    set() {
      sessionStorage.setItem(key, JSON.stringify({ ...dataObj, ...form }))
    },
  }
}
